/*
 * @Author: Suo-Yuheng syh15399227325@163.com
 * @Date: 2025-09-09 17:19:53
 * @LastEditors: Suo-Yuheng syh15399227325@163.com
 * @LastEditTime: 2025-09-10 10:48:06
 * @FilePath: /0909/Protocol.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

// #define MySelf 1

const std::string blank_space_sep = " ";
const std::string protocol_sep = "\n";

std::string Encode(std::string &content) // 给报文 封装报头，和报文最后的 分割符
{
    std::string package = std::to_string(content.size());
    package += protocol_sep;
    package += content;
    package += protocol_sep; // 报文最后的分割符

    return package;
}

// "len"\n"x op y"\n   --->  "x op y"
bool Decode(std::string &package, std::string *content)
{
    size_t first_sep = package.find(protocol_sep);
    if (first_sep == std::string::npos)
        return false;
    std::string part_len = package.substr(0, first_sep);
    size_t len = stoi(part_len);

    size_t total_len = part_len.size() + len + 2; // 完整报文长度
    if (package.size() < total_len)
        return false; // 数据不足，报文不完整

    *content = package.substr(first_sep + 1, len);
    // erase 移除报文 package.erase(0, total_len);
    package.erase(0, total_len);

    return true;
}

struct Request
{
    Request(int data1, int data2, char oper)
        : x(data1), y(data2), op(oper)
    {
    }
    Request()
    {
    }

    bool Serialize(std::string *out) // 输出型参数
    {
#ifdef MySelf
        // 构建报文的 有效载荷
        // struct  --->  string,  "x op y"
        std::string s = std::to_string(x);
        s += blank_space_sep;
        s += op;
        s += blank_space_sep;
        s += std::to_string(y);

        *out = s;
        return true;

#else
        Json::Value root;
        root["x"] = x;
        root["y"] = y;
        root["op"] = op;
        // Json::FastWriter w;
        Json::StyledWriter w;
        *out = w.write(root);
        return true;

#endif
    }

    bool Deserialize(const std::string &in)
    {
#ifdef MySelf
        // "x op y"
        size_t first_blank = in.find(blank_space_sep);
        if (first_blank == std::string::npos)
            return false;
        std::string part_x = in.substr(0, first_blank);

        size_t second_blank = in.find(blank_space_sep, first_blank + 1);
        if (second_blank == std::string::npos)
            return false;
        std::string part_op = in.substr(first_blank + 1, 1);
        std::string part_y = in.substr(second_blank + 1);

        x = std::stoi(part_x);
        y = std::stoi(part_y);
        op = part_op[0];

        return true;

#else
        Json::Value v;
        Json::Reader r;
        r.parse(in, v);

        x = v["x"].asInt();
        y = v["y"].asInt();
        op = v["op"].asInt();
        return true;

#endif
    }

    void DebugPrint()
    {
        std::cout << "新请求构建完成：  " << x << op << y << "= ? " << std::endl;
    }

    // x op y
    int x;
    int y;
    char op; // + - * / %
};

struct Response
{
    Response(int res, int c)
        : result(res), code(c)
    {
    }
    Response()
    {
    }

    bool Serialize(std::string *out) // 输出型参数
    {
#ifdef MySelf
        // struct  --->  string,  "result code"
        // 构建报文的有效载荷
        std::string s = std::to_string(result);
        s += blank_space_sep;
        s += std::to_string(code);

        *out = s;
        return true;

#else
        Json::Value root;
        root["result"] = result;
        root["code"] = code;

        // Json::FastWriter w;
        Json::StyledWriter w;
        *out = w.write(root);
        return true;
#endif
    }

    bool Deserialize(const std::string &in)
    {
#ifdef MySelf
        // "result code"
        size_t pos = in.find(blank_space_sep);
        if (pos == std::string::npos)
            return false;

        std::string part_result = in.substr(0, pos);
        std::string part_code = in.substr(pos + 1);
        result = std::stoi(part_result);
        code = std::stoi(part_code);

        return true;
#else
        Json::Value root;
        Json::Reader r;
        r.parse(in, root);

        result = root["result"].asInt();
        code = root["code"].asInt();
        return true;
#endif
    }

    void DebugPrint()
    {
        std::cout << "结果响应完成: result: " << result << ", code: " << code << std::endl;
    }

    int result;
    int code; // 0，可信；否则结果不可信
};